package com.hxkcloud.nurse.views;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.tubb.smrv.SwipeMenuHelper;
import com.hxkcloud.nurse.views.pulltorefreshlayout.Pullable;
/**
 * 滑动删除
 * */
public class SwipeMenuRecyclerView extends RecyclerView implements SwipeMenuHelper.Callback, Pullable {

    protected SwipeMenuHelper mHelper;
    private Adapter mAdapter;
    private LayoutManager mLayoutManager;
    private boolean canRefresh = true;//是否可以下拉刷新，默认可以
    private boolean canLoadMore = true;//是否可以上拉加载，默认可以

    public SwipeMenuRecyclerView(Context context) {
        super(context);
        init();
    }

    public SwipeMenuRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SwipeMenuRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    protected void init() {
        mHelper = new SwipeMenuHelper(getContext(), this);
        setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isIntercepted = super.onInterceptTouchEvent(ev);
        // ignore Multi-Touch
        if (ev.getActionIndex() != 0) return true;
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                isIntercepted = mHelper.handleListDownTouchEvent(ev, isIntercepted);
                break;
        }
        return isIntercepted;
    }

    public int getPositionForView(View touchView) {
        return getChildAdapterPosition(touchView);
    }

    @Override
    public int getRealChildCount() {
        return getChildCount();
    }

    @Override
    public View getRealChildAt(int index) {
        return getChildAt(index);
    }

    @Override
    public View transformTouchingView(int touchingPosition, View touchingView) {
        ViewHolder vh = findViewHolderForAdapterPosition(touchingPosition);
        if (vh != null) {
            return vh.itemView;
        }
        return touchingView;
    }

    public void setCanRefresh(boolean canRefresh) {
        this.canRefresh = canRefresh;
    }

    public void setCanLoadMore(boolean canLoadMore) {
        this.canLoadMore = canLoadMore;
    }

    @Override
    public boolean canPullDown() {
        mAdapter = getAdapter();
        mLayoutManager = getLayoutManager();
        if (mAdapter != null && mLayoutManager != null) {
            if (canRefresh) {
                if (mAdapter.getItemCount() == 0) {//没有数据的时候也可以刷新
                    return true;
                } else if (mLayoutManager.findViewByPosition(0) != null
                        && mLayoutManager.findViewByPosition(0).getTop() >= 0) {//当前item是第一个item的时候也可以刷新
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean canPullUp() {
        mAdapter = getAdapter();
        mLayoutManager = getLayoutManager();
        if (mAdapter != null && mLayoutManager != null) {
            if (canLoadMore) {
                int lastPosition = mAdapter.getItemCount() - 1;
                if (mAdapter.getItemCount() == 0) {//没有数据的时候也可以刷新
                    return true;
                } else if (mLayoutManager.findViewByPosition(lastPosition) != null && mLayoutManager.findViewByPosition(lastPosition).getBottom() <= getMeasuredHeight()) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
